classdef Samples < Signal

properties (SetAccess = protected)
	Data;
	NumberOfSamples; % Sample Number per Chip * ( NumberOfChip + 1)
	Time;
	Date;
	FileFormat;
	FrequencyOffset;
	Bandwidth;
	SamplingPeriod;
	SamplesFileName;
	SamplesFile;
end % end of properties

properties (Constant)
	InfoFileName = 'snapshots/snapshot_info.txt';
end % end of properties

methods
	
	function obj = Samples(Name, SamplesFileName)
		obj = obj@Signal(Name);
		obj.SamplesFileName = SamplesFileName;

		ImportedData = importdata(obj.InfoFileName);
		[m n] = size(ImportedData.textdata);
		FileFound = 0;
		for i=1:m
			if (strfind(obj.SamplesFileName, ImportedData.textdata{i,1}) > 0)
				FileFound = 1;
				break;
			end
		end
		if (~FileFound)
			error('Snapshot file not found')
		end
		obj.Time = ImportedData.textdata{i,3};
		obj.Date = ImportedData.textdata{i,2};
		obj.FileFormat = ImportedData.textdata{i,4};
		obj.FrequencyOffset = ImportedData.data(i,1);
		obj.Bandwidth = ImportedData.data(i,2);
		obj.SamplingPeriod = 1/ImportedData.data(i,3);
		obj.SamplesFile = fopen(obj.SamplesFileName, 'r');

 	end

	function obj = loadSamples(obj, NumberOfSamples)
		switch (obj.Name)
			case 'E5'
				NumbersToRead = 4;
			case 'E5a'
				NumbersToRead = 2;
			case 'E5aI'
				NumbersToRead = 2;
			case 'E5aQ'
				NumbersToRead = 2;
			case 'E5b'
				NumbersToRead = 2;
			case 'E5bI'
				NumbersToRead = 2;
			case 'E5bQ'
				NumbersToRead = 2;
			case 'E1'
				NumbersToRead = 2;
			case 'E1b'
				NumbersToRead = 2;
			case 'E1c'
				NumbersToRead = 2;
			case 'B1'
				NumbersToRead = 2;
			otherwise
				error('Unknown signal')
		end
			
		NumbersToStore = NumbersToRead/2;
		obj.Data = zeros(NumberOfSamples, NumbersToStore); 
		obj.NumberOfSamples = 0;
		switch (NumbersToRead)
			case 2
				switch (obj.FileFormat)
					case 'txt'
						for k=1:NumberOfSamples
							tmp = fscanf(obj.SamplesFile, ' %d, %d\n', 2);
							obj.Data(k,1) = tmp(1) + j*tmp(2);
							obj.NumberOfSamples = obj.NumberOfSamples +1;
						end
					case 'bin'
						for k=1:NumberOfSamples
							tmp = fread(obj.SamplesFile, 2, 'int8');
							obj.Data(k,1) = tmp(1) + j*tmp(2);
							obj.NumberOfSamples = obj.NumberOfSamples +1;
						end
					otherwise
						error('Unknown text format')
				end
			case 4
				switch (obj.FileFormat)
					case 'txt'
						for k=1:NumberOfSamples
							tmp = fscanf(obj.SamplesFile, ' %d, %d\n', 2);
							if (tmp(1) < 0) 
								tmp(1) = tmp(1) + 256;
							end
							if (tmp(2) < 0) 
								tmp(2) = tmp(2) + 256;
							end
							obj.Data(k,2) = 	Samples.bit2num(bitget(tmp(1),4:(-1):1)) +...
															j*Samples.bit2num(bitget(tmp(1),8:(-1):5));
							obj.Data(k,1) = 	Samples.bit2num(bitget(tmp(2),4:(-1):1)) +...
															j*Samples.bit2num(bitget(tmp(2),8:(-1):5));
							obj.NumberOfSamples = obj.NumberOfSamples +1;
						end
					case 'bin'
						% TODO
					otherwise
						error('Unknown file format')
				end
			otherwise
				error('Uknown numbers to read per line')
		end
	end

	function delete(obj)
		if (~isempty(obj.SamplesFile)) 
			fclose(obj.SamplesFile);
		end
	end
	
end % end of methods

methods (Static)
	
	function out = bit2num(bits)
		out = bin2dec(num2str(bits));
	end

end % end of methods

end % end of class
